package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.driver.minio.MinioProperties;
import com.atguigu.daijia.driver.service.CosService;
import com.atguigu.daijia.model.vo.driver.CosUploadVo;
import io.minio.*;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CosServiceImpl implements CosService {

//    @Autowired
//    private TencentCloudProperties tencentCloudProperties;

//    @Autowired
//    private CiService ciService;

//    @Autowired
//    private MinioClient client;

    @Autowired
    private MinioProperties properties;

    @Override
    public CosUploadVo upload(MultipartFile file , String path) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        //初始化minio
        MinioClient client = MinioClient.builder()
                .endpoint(properties.getEndpointUrl())
                .credentials(properties.getAccessKey(), properties.getSecretKey()).build();

        //创建桶
        boolean bucketExists = client.bucketExists(BucketExistsArgs.builder()
                .bucket(properties.getBucketName()).build());
        if(!bucketExists){
            client.makeBucket(MakeBucketArgs.builder().bucket(properties.getBucketName()).build());
            client.setBucketPolicy(SetBucketPolicyArgs.builder()
                    .bucket(properties.getBucketName()).build());
        }

        String filename = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()
                        + "/" + UUID.randomUUID()
                        + "-" + file.getOriginalFilename());
        //上传文件
        client.putObject(PutObjectArgs.builder().bucket(properties.getBucketName())
                        .object(filename)
                        .stream(file.getInputStream(), file.getSize(), -1)
                        .contentType(file.getContentType()).build());
//        //返回vo对象
//        CosUploadVo cosUploadVo = new CosUploadVo();
//        cosUploadVo.setUrl(uploadPath);
//        //图片临时访问url，回显使用
//        String imageUrl = this.getImageUrl(uploadPath);
//        cosUploadVo.setShowUrl(imageUrl);
//        return cosUploadVo;
        CosUploadVo cosUploadVo = new CosUploadVo();
        cosUploadVo.setUrl(filename);
        cosUploadVo.setShowUrl(filename);
        return cosUploadVo;
    }

//    public COSClient getCosClient(){
//        String accessKey = properties.getAccessKey();
//        String secretKey = properties.getSecretKey();
//    }

//    @Override
//    public CosUploadVo upload(MultipartFile file, String path) {
//        //获取cosClient对象
//        COSClient cosClient = this.getCosClient();
//        //文件上传
//        //元数据信息
//        ObjectMetadata meta = new ObjectMetadata();
//        meta.setContentLength(file.getSize());
//        meta.setContentEncoding("UTF-8");
//        meta.setContentType(file.getContentType());
//
//        //向存储桶中保存文件
//        String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); //文件后缀名
//        String uploadPath = "/driver/" + path + "/" + UUID.randomUUID().toString().replaceAll("-", "") + fileType;
//        // 01.jpg
//        // /driver/auth/0o98754.jpg
//        PutObjectRequest putObjectRequest = null;
//        try {
//            //1 bucket名称
//            //2
//            putObjectRequest = new PutObjectRequest(tencentCloudProperties.getBucketPrivate(),
//                    uploadPath,
//                    file.getInputStream(),
//                    meta);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        putObjectRequest.setStorageClass(StorageClass.Standard);
//        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest); //上传文件
//        cosClient.shutdown();
//
//        //图片审核
//        Boolean imageAuditing = ciService.imageAuditing(uploadPath);
//        if(!imageAuditing) {
//            //删除违规图片
//            cosClient.deleteObject(tencentCloudProperties.getBucketPrivate(),uploadPath);
//            throw new GuiguException(ResultCodeEnum.IMAGE_AUDITION_FAIL);
//        }
//
//        //返回vo对象
//        CosUploadVo cosUploadVo = new CosUploadVo();
//        cosUploadVo.setUrl(uploadPath);
//        //图片临时访问url，回显使用
//        String imageUrl = this.getImageUrl(uploadPath);
//        cosUploadVo.setShowUrl(imageUrl);
//        return cosUploadVo;
//    }

//    public COSClient getCosClient() {
//        String secretId = tencentCloudProperties.getSecretId();
//        String secretKey = tencentCloudProperties.getSecretKey();
//        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
//        // 2 设置 bucket 的地域, COS 地域
//        Region region = new Region(tencentCloudProperties.getRegion());
//        ClientConfig clientConfig = new ClientConfig(region);
//        // 这里建议设置使用 https 协议
//        clientConfig.setHttpProtocol(HttpProtocol.https);
//        // 3 生成 cos 客户端。
//        COSClient cosClient = new COSClient(cred, clientConfig);
//        return cosClient;
//    }

    //获取临时签名URL
    @Override
    public String getImageUrl(String path) {
//        if(!StringUtils.hasText(path)) return "";
        return " ";
        //获取client对象
//        client.getObject(GetObjectArgs.builder().bucket(properties.getBucketName()),)

//        //获取cosclient对象
//        COSClient cosClient = this.getCosClient();
//        //GeneratePresignedUrlRequest
//        GeneratePresignedUrlRequest request =
//                new GeneratePresignedUrlRequest(tencentCloudProperties.getBucketPrivate(),
//                         path, HttpMethodName.GET);
//        //设置临时URL有效期为15分钟
//        Date date = new DateTime().plusMinutes(15).toDate();
//        request.setExpiration(date);
//        //调用方法获取
//        URL url = cosClient.generatePresignedUrl(request);
//        cosClient.shutdown();
//        return url.toString();
    }
}
